
package com.ifcar.app.activity.mycomment;

import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class MyCommentReply implements OnClickListener, OnFocusChangeListener {
    public static final int ACTION_REPLY = 0;

    private Activity mActivity;

    private View mContainer;

    private EditText mReplyEdittext;

    private Button mReplyButton;

    private ActionCallback mCallback;

    private Object mEditTag;

    public MyCommentReply(Activity activity, View container) {
        mActivity = activity;
        mContainer = container;

        mReplyEdittext = (EditText) container.findViewById(R.id.reply_edittext);
        mReplyEdittext.setOnFocusChangeListener(this);
        mReplyButton = (Button) container.findViewById(R.id.reply_button);
        mReplyButton.setOnClickListener(this);
    }

    public void setCallback(ActionCallback callback) {
        mCallback = callback;
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        final int keyCode = event.getKeyCode();
        final int action = event.getAction();

        if (mContainer.getVisibility() == View.VISIBLE && checkDismissKey(keyCode)) {
            if (action == KeyEvent.ACTION_UP) {
                finishEdit();
            }
            return true;
        }
        return false;
    }

    protected boolean checkDismissKey(int keyCode) {
        return keyCode == KeyEvent.KEYCODE_BACK;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (dispatchClick(id)) {
            return;
        }
    }

    public boolean dispatchClick(int id) {
        boolean ret = true;
        switch (id) {
            case R.id.reply_button:
                callCallback(ACTION_REPLY);
                break;
            default:
                ret = false;
                break;
        }
        return ret;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            finishEdit();
        }

    }

    public void beginEdit(Object editTag) {
        mContainer.setVisibility(View.VISIBLE);
        mEditTag = editTag;
        mReplyEdittext.requestFocus();

        InputMethodManager imm = (InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(mReplyEdittext, 0);
        }
    }

    public void finishEdit() {
        mContainer.setVisibility(View.GONE);
        mEditTag = null;
        mReplyEdittext.setText("");

        InputMethodManager imm = (InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mReplyEdittext.getWindowToken(), 0);
    }

    public String getReplyContent() {
        return mReplyEdittext.getText().toString();
    }

    private void callCallback(int action) {
        if (mCallback != null) {
            mCallback.onReplyAction(this, action);
        }
    }

    public Object getEditTag() {
        return mEditTag;
    }

    public interface ActionCallback {
        public void onReplyAction(MyCommentReply commonReply, int action);
    }

    public void onApplyTheme(Context context, ThemeSettingsHelper themeSettingsHelper) {
        themeSettingsHelper.setViewBackgroud(context, mContainer, R.drawable.reply_frame_bk);

        themeSettingsHelper.setTextViewColor(context, mReplyButton, R.color.list_title_color);
        themeSettingsHelper.setViewBackgroud(context, mReplyButton, R.drawable.netease_big_button);
    }
}
